package com.wx.bootsecurity.controller;


import com.wx.bootsecurity.common.CommonResult;
import com.wx.bootsecurity.entity.vo.LoginParams;
import com.wx.bootsecurity.service.WxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author 阿尔法哲
 * @since 2021-07-22
 */
@RestController
@RequestMapping("/user")
public class WxUserController {

    @Value("${jwt.tokenHead}")
    String tokenHead;

    @Autowired
    private WxUserService userService;

    @PostMapping("/tologin")
    public CommonResult tologin(LoginParams loginParams){

        HashMap<String, String> data = new HashMap<>();
        String token = null;
        try {
            token = userService.tologin(loginParams);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("用户名或密码错误");
        }
        if (StringUtils.isEmpty(token)){
            return CommonResult.failed("用户名或密码错误");
        }
        data.put("tokenHead",tokenHead);
        data.put("access_token",token);
        // localStorage.setItem("Authorization","Bearer sdsdfdfds")
        // $ajax{data:{},type:"",header:{"Authorization":"Bearer sdsdfdfds"}}
        return CommonResult.success(data);
    }

}

